package app.qrcode.browser.todoapp.fragments.add

import android.os.Bundle
import android.view.*
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import app.qrcode.browser.todoapp.R
import app.qrcode.browser.todoapp.common.ViewBindingFragment
import app.qrcode.browser.todoapp.data.models.ToDoData
import app.qrcode.browser.todoapp.data.viewmodel.ToDoViewModel
import app.qrcode.browser.todoapp.databinding.FragmentAddBinding
import app.qrcode.browser.todoapp.fragments.SharedViewModel
import app.qrcode.browser.todoapp.fragments.update.DatePickerFragment
import app.qrcode.browser.todoapp.fragments.update.TimePickerFragment

class AddFragment : ViewBindingFragment<FragmentAddBinding>(), DatePickerFragment.DateChosen,
    TimePickerFragment.TimeChosen {

    private val mToDoViewModel: ToDoViewModel by activityViewModels()
    private val mSharedViewModel: SharedViewModel by activityViewModels()


    override fun initView() {
        super.initView()
        setHasOptionsMenu(true)
        binding.prioritiesSpinner.onItemSelectedListener = mSharedViewModel.listener
        binding.taskStatusSpinner.onItemSelectedListener = mSharedViewModel.listener
        binding.setDateTv.setOnClickListener {
            var datePickerFragment = DatePickerFragment()
            datePickerFragment.run {
                setDateChosen(this@AddFragment)
                show(this@AddFragment.parentFragmentManager, "datePicker")
            }


        }
        binding.setTimeTv.setOnClickListener {
            var timePickerFragment = TimePickerFragment()
            timePickerFragment.run {
                setTimeChosen(this@AddFragment)
                show(this@AddFragment.parentFragmentManager, "timePicker")
            }
        }
    }

    override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
        inflater.inflate(R.menu.add_fragment_menu, menu)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if (item.itemId == R.id.menu_add) {
            insertDataToDb()
        }
        return super.onOptionsItemSelected(item)
    }

    private fun insertDataToDb() {
        val mTitle = binding.titleEt.text.toString()
        val mPriority = binding.prioritiesSpinner.selectedItem.toString()
        val mDescription = binding.descriptionEt.text.toString()
        val mTaskStatus = binding.taskStatusSpinner.selectedItem.toString()
        val mDate =
            if (binding.setDateTv.text.toString() != "Set Date") binding.setDateTv.text.toString() else null
        val mTime =
            if (binding.setTimeTv.text.toString() != "Set Time") binding.setTimeTv.text.toString() else null
        val isNoticed = binding.noticeSwitch.isChecked

        val validation = mSharedViewModel.verifyDataFromUser(mTitle, mDescription)
        if (validation) {
            //Insert Data to Database
            val newData = ToDoData(
                0,
                mTitle,
                mSharedViewModel.parsePriority(mPriority),
                mSharedViewModel.parseTaskStatus(mTaskStatus),
                mDescription,
                date = mDate,
                time = mTime,
                isNoticed = isNoticed
            )
            mToDoViewModel.insertData(newData)
            Toast.makeText(requireContext(), "Successfully added", Toast.LENGTH_SHORT).show()
            findNavController().navigate(R.id.action_addFragment_to_listFragment)
        } else {
            Toast.makeText(requireContext(), "Please fill out all field", Toast.LENGTH_SHORT).show()
        }
    }

    override fun setDate(str: String) {
        binding.setDateTv.text = str
    }

    override fun setTime(str: String) {
        binding.setTimeTv.text = str
    }


}